<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\builder\TableBuilder;
use app\common\model\Assets;
use think\App;

class Bonus extends Base
{

    //主键，默认为id
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\Account';

    //搜索条件
    protected $search;

    //额外查询条件
    protected $extraSearch = ['is_admin', 'is_bonus'];

    //关联模型
    protected $hasOne = ['user', 'take'];

    //页面所需表头参数，渲染相关参数参考layui官方文档，目前暂时为静态，后期考虑存库
    protected $table;

    public function index()
    {
        $this->search = [
            ['uid', '会员编号/昵称/手机号', 'input', '', '\app\common\model\User', 'code|nickname|mobile,id'],
            ['source', '来源会员编号/昵称/手机号', 'input', '', '\app\common\model\User', 'code|nickname|mobile,id'],
            ['type', '账户类型', 'select', Assets::getFields(3)],
        ];
        return $this->setTable();
    }

    public function bonus()
    {
        $this->search = [
            ['uid', '会员编号/昵称/手机号', 'input', '', '\app\common\model\User', 'code|nickname|mobile,id'],
            ['source', '来源会员编号/昵称/手机号', 'input', '', '\app\common\model\User', 'code|nickname|mobile,id'],
            ['type', '账户类型', 'select', Assets::getFields(3)],
            ['category', '奖金类型', 'select', [2 => '释放', 3 => '推荐奖']]
        ];
        return $this->setTable(['is_bonus' => 1]);
    }

    public function recharge()
    {
        $this->search = [
            ['uid', '会员编号/昵称/手机号', 'input', '', '\app\common\model\User', 'code|nickname|mobile,id'],
            ['type', '账户类型', 'select', Assets::getFields(3)],
        ];
        return $this->setTable(['is_admin' => 1], 1);
    }

    public function setTable($extra_map = [], $is_recharge = 0)
    {
        $this->table = [
            ['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80],
            ['title' => '昵称', 'templet' => '<div>{{d.user.nickname}}</div>'],
            ['title' => '编号', 'templet' => '<div>{{d.user.code}}</div>'],
            ['title' => '手机号', 'templet' => '<div>{{d.user.mobile}}</div>'],
            ['field' => 'type', 'title' => '账户'],
            ['field' => 'front', 'title' => '前余额'],
            ['title' => '金额', 'templet' => '<div><span class="color color_{{d.fx}}">{{d.fx}}{{d.amount}}</span></div>'],
            ['field' => 'after', 'title' => '后余额'],
            ['field' => 'create_time', 'title' => '时间'],
            ['field' => 'intro', 'title' => '说明'],
        ];
        //后台充值明细
        if ($is_recharge == 1) {
            $this->table[] = ['field' => 'msg', 'title' => '备注'];
        } else {
            $add = [
                ['title' => '来源会员编号', 'templet' => '<div>{{d.is_user == 1 ? d.take.code:"-"}}</div>'],
                ['title' => '来源会员姓名', 'templet' => '<div>{{d.is_user == 1 ? d.take.nickname:"-"}}</div>'],
                ['title' => '来源会员手机号', 'templet' => '<div>{{d.is_user == 1 ? d.take.mobile:"-"}}</div>'],
                ['title' => '状态', 'templet' => '<div>{{d.is_grant == 2?"正常":"-"}}</div>', 'fixed' => 'right', 'align' => 'center']
            ];
            foreach ($add as $v) {
                $this->table[] = $v;
            }
        }

        return TableBuilder::getInstance()
            ->addColumns($this->table)
            ->setSearch($this->search)
            ->addExtraMap($extra_map)
            ->addTopButtons(['export'])
            ->fetch();
    }


    public function export()
    {
        $s = $this->setMap();
        $s['with'] = $this->hasOne;
        $title = ['id' => 'ID', 'nickname' => '昵称', 'code' => '编号',
            'mobile' => '手机号', 'type' => '账户', 'amount' => '金额', 'front' => '前余额', 'after' => '后余额',
            'create_time' => '时间', 'intro' => '说明'];
        $name = '后台充值明细' . date('Y-m-d H:i:s');
        if (!isset($s['param']['is_admin'])) {
            $add = ['take_code' => '来源会员编号', 'take_nickname' => '来源会员姓名',
                'take_mobile' => '来源会员手机号'];
            $title = array_merge($title, $add);
            if (!isset($s['param']['is_bonus'])) {
                $name = '账户明细' . date('Y-m-d H:i:s');
            } else {
                $name = '奖金明细' . date('Y-m-d H:i:s');
            }

        }

        $s['filed'] = 'id,uid,type,intro,amount,create_time,source,fx,is_user,front,after';
        $list = $this->model::getSelect($s);

        foreach ($list['data'] as &$v) {
            $v['code'] = $v->user->code;
            $v['nickname'] = $v->user->nickname;
            $v['mobile'] = $v->user->mobile;
            $v['amount'] = $v->fx . $v->amount;
            if (!isset($s['param']['is_admin'])) {
                $v['take_code'] = $v->is_user ? $v->take->code : '-';
                $v['take_nickname'] = $v->is_user ? $v->take->nickname : '-';
                $v['take_mobile'] = $v->is_user ? $v->take->mobile : '-';
            }
            unset($v->is_user);
            unset($v->uid);
            unset($v->fx);
            unset($v->user);
            unset($v->take);
            unset($v->source);
        }

        return json(['code' => 1, 'result' => ['data' => $list['data']->toArray(), 'title' => $title, 'name' => $name]]);
    }

}
